package demo.practice.top100;

import java.util.HashMap;
import java.util.Map;

public class P1 {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            //if(!map.containsKey(target - nums[i]))
            map.put(target - nums[i], i);
        }

        for (int i = 0; i < nums.length; i++) {
            int b;
            if (map.containsKey(nums[i]) && (b = map.get(nums[i])) != i) {
                return new int[]{i, map.get(nums[i])};
            }
        }

        return new int[]{-1, -1};
    }

    public int[] twoSum2(int[] nums, int target) {

        Map<Integer, Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(nums[i])){
                return new int[]{i, map.get(nums[i])};
            }

            map.put(target-nums[i],i  );
        }
        return new int[]{-1, -1};
    }


}
